/**
 * Created by lenovo on 2016/4/15.
 */
public class Complex {
    double real,img;    //实部和虚部

    public Complex()        //默认构造方法
    {
        this.real=0;
        this.img =0;
    }
    public Complex(double real,double img)    //带参数的构造方法
    {
        this.real=real;
        this.img =img;
    }
    public double getReal()
    { return this.real;    }        //得到实部


    public double getImage()
    { return this.img;    }        //得到虚部
    public double getReal(Complex c)
    {    return c.real;        }        //得到复数c的实部，这两个函数看起来好像有点多余，但在特殊的情况下会有用


    public double getImage(Complex c)
    {    return c.img;            }        //得到复数c的虚部


    public void setReal    (double real)
    {    this.real=real;        }        //设置实部


    public void setImage(double img)
    {    this.img =img;        }        //设置虚部



    public Complex addComplex(Complex a,Complex b)    //两个复数相加，结果返回
    {
        Complex temp =new Complex();
        temp.real=a.real+b.real;
        temp.img =a.img +b.img;
        return temp;
    }
    public Complex decComplex(Complex a,Complex b)    //两个复数相减，结果返回
    {
        Complex temp = new Complex();
        temp.real = a.real - b.real;
        temp.img  = a.img  - b.img;
        return temp;
    }
    public Complex mulComplex(Complex a,Complex b)    //两个复数相乘，结果返回
    {
        Complex temp = new Complex();
        temp.real = a.real*b.real-a.img*b.img;
        temp.img    = a.real*b.img+a.img*b.real;
        return temp;
    }
    public Complex divComplex(Complex a,Complex b)    //两个复数相除，结果返回
    {
        Complex temp = new Complex();
        temp.real=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
        temp.img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
        return temp;
    }

    public void printComplex()        //在console端输出这个复数,测试用
    {
        System.out.println(""+this.real+"+"+this.img+"i");//这里可以填加一点代码以判断虚部的正负，这个工作我没有做
    }

    public String toString(){

        String fin=" ";
        if(img>0){
            fin = real+"+"+img+"i";
        }else if(img<0){
            fin = real+ ""+img+"i";
        }else{
            fin = fin;
        }
        return  fin;
    }

    public static void main(String[] args)        //测试代码
    {
        Complex cc=new Complex(4,8);
        cc.printComplex();
        Complex dd=new Complex(2,2);
        dd.printComplex();
        System.out.println("-----------------");
        Complex ff=new Complex();

        ff=ff.addComplex(cc,dd);
        ff.printComplex();
        ff=ff.decComplex(cc,dd);
        ff.printComplex();
        ff=ff.mulComplex(cc,dd);
        ff.printComplex();
        ff=ff.divComplex(cc,dd);
        ff.printComplex();
        System.out.println("-----------------");
        cc.printComplex();
        cc=new Complex(4,8);
        cc.printComplex();
        cc=new Complex(4,8);
        cc.printComplex();
        cc=new Complex(4,8);
        cc.printComplex();
        System.out.println("-----------------");
        cc.setReal(123);
        cc.setImage(456);
        cc.printComplex();
        System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
        System.out.println("-----------------");
    }
}
